import Vue from 'vue'

/**
 * 移除数组中的元素
 * @param arrays
 */
Vue.prototype.$ySpliceAll = function (...arrays) {
  for (let item of arrays){
    item.splice(0,item.length);
  }
};

Vue.prototype.$yPushAll = function (targetArray,datas) {

  targetArray.splice(0,targetArray.length);
  datas.forEach(function (item) {
    targetArray.push(item)
  })
};

Vue.prototype.$yFreeze = function(value){
  return Object.freeze(value);

};

Vue.prototype.$yMobile = function(){
  var sUserAgent = window.navigator.userAgent;
  if (sUserAgent.indexOf('Android') > -1 || sUserAgent.indexOf('iPhone') > -1 || sUserAgent.indexOf('iPad') > -1 || sUserAgent.indexOf('iPod') > -1 || sUserAgent.indexOf('Symbian') > -1) {
    return true;
  } else {
    return false;
  }
};


Vue.prototype.$yFirstUpperCase = function(val){
  return val.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
    return $1.toUpperCase() + $2.toLowerCase();
  });
};


Array.prototype.yRemove = function(val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

Array.prototype.yRemoveIndex = function(index){
  this.splice(index,1);
};

Array.prototype.yIndexOf = function(val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i] == val) return i;
  }
  return -1;
};

Array.prototype.yHas = function(val) {
  return this.yIndexOf(val)!==-1;

};



String.prototype.ResetBlank=function(){
  var regEx = /\s+/g;
  return this.replace(regEx, ' ');
};
Vue.prototype.$getDictLabel  =  function(dictType,value,defaultLable='') {
  var stateStr = '';
  this.$store.getters.dictionaries[dictType].forEach(function (item) {
    if (item.value === value) {
      stateStr = item.name;
      return;
    }
  });
  if(stateStr==''&&defaultLable!=''){
    stateStr = defaultLable;
  }
  return stateStr;
};
